// pages/agency/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsnum: [{
      num: 0
    }, {
      num: 0
    }, {
      num: 0
    }, {
      num: 0
    }, {
      num: 0
    }, {
      num: 0
    }, {
      num: 0
    }, {
      num: 0
    }, {
      num: 0
    }, {
      num: 0
    }],
    gooodlist: "",
    zonjia: 0,
    payData: [],
    aarr:[],
    aarrlength:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //请求商品列表
    wx.request({
      url: app.globalData.url+'/zkxj/good/list',
      data: {
        "currPage": 1,
        "pageSize": 10,
        "goodArea": 0,
        "putawayStatus": 1,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        this.setData({
          gooodlist: res.data.data.list,
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  handledetodelait(e) {
    const statet = e.currentTarget.dataset['state'] || 0
    console.log(statet);
    wx.navigateTo({
      url: `/pages/gooddelait/index?id=${statet}`,
    })
  },
  handledelae(e) {
    const index = e.currentTarget.dataset['index'] || 0
    const price = e.currentTarget.dataset['price'];
    let id=e.currentTarget.dataset['id']
    console.log(price);
  
    //判断加减按钮
    
    if (this.data.goodsnum[index].num > 0) {
      this.data.goodsnum[index].num = --this.data.goodsnum[index].num
      //计算减法
      let allprc = this.data.zonjia - price
      this.setData({
        goodsnum: this.data.goodsnum,
        zonjia: allprc
      })
     
      for (let k = 0; k < this.data.aarr.length; k++) {
       console.log(this.data.aarr[k]);
        if(this.data.aarr[k].id==id){
          console.log();
          //将数组递减
          if(this.data.aarr[k]){
    
            if(this.data.aarr[k].num>0){
              this.data.aarr[k].num=this.data.aarr[k].num-1
            }
             if(this.data.aarr[k].num==0){
              this.data.aarr.splice(k, 1);
            }
          }
        }
      }
      this.setData({
        aarrlength:this.data.aarr.length
      })
      console.log(this.data.aarr);
    } else {
      wx.showToast({
        title: '请至少选择一个商品！',
        icon: 'error',
        duration: 1000,
        mask: true
      })
    }
    // console.log(this.data.goodsnum);

  },
  handledeadd(e) {
    var that = this.data
    //判断加减按钮
    const index = e.currentTarget.dataset['index'] || 0
    const price = e.currentTarget.dataset['price'];
    // let addall=price*(this.data.goodsnum[index].num+1)
    // console.log(addall);
    //计算加得价格
    let allprc = price + this.data.zonjia
    this.data.goodsnum[index].num = ++this.data.goodsnum[index].num
    
    this.setData({
      goodsnum: this.data.goodsnum,
      zonjia: allprc
    })
    // console.log(this.data.goodsnum);
    //先将数组循环判断是否已有该条数据
    if (index == 0) {
      that.payData.push({
        id: that.gooodlist[0].id,
        num: that.goodsnum[index].num || 1,
        oldpric: that.gooodlist[0].originalPrice,
        price: that.gooodlist[0].price,
        name: that.gooodlist[0].name,
        coverPic: that.gooodlist[0].coverPic
      })
    } else if (index == 1) {
      that.payData.push({
        id: that.gooodlist[1].id,
        num: that.goodsnum[index].num || 1,
        oldpric: that.gooodlist[1].originalPrice,
        price: that.gooodlist[1].price,
        name: that.gooodlist[1].name,
        coverPic: that.gooodlist[1].coverPic
      })
    } else if (index == 2) {
      that.payData.push({
        id: that.gooodlist[2].id,
        num: that.goodsnum[index].num ||1 ,
        oldpric: that.gooodlist[2].originalPrice,
        price: that.gooodlist[2].price,
        name: that.gooodlist[2].name,
        coverPic: that.gooodlist[2].coverPic
      })
    }else if (index == 3) {
      that.payData.push({
        id: that.gooodlist[3].id,
        num: that.goodsnum[index].num ||1 ,
        oldpric: that.gooodlist[3].originalPrice,
        price: that.gooodlist[3].price,
        name: that.gooodlist[3].name,
        coverPic: that.gooodlist[3].coverPic
      })
    }else if (index == 4) {
      that.payData.push({
        id: that.gooodlist[4].id,
        num: that.goodsnum[index].num ||1 ,
        oldpric: that.gooodlist[4].originalPrice,
        price: that.gooodlist[4].price,
        name: that.gooodlist[4].name,
        coverPic: that.gooodlist[4].coverPic
      })
    }else if (index == 5) {
      that.payData.push({
        id: that.gooodlist[5].id,
        num: that.goodsnum[index].num ||1 ,
        oldpric: that.gooodlist[5].originalPrice,
        price: that.gooodlist[5].price,
        name: that.gooodlist[5].name,
        coverPic: that.gooodlist[5].coverPic
      })
    }else if (index == 6) {
      that.payData.push({
        id: that.gooodlist[6].id,
        num: that.goodsnum[index].num ||1 ,
        oldpric: that.gooodlist[6].originalPrice,
        price: that.gooodlist[6].price,
        name: that.gooodlist[6].name,
        coverPic: that.gooodlist[6].coverPic
      })
    }else if (index == 7) {
      that.payData.push({
        id: that.gooodlist[7].id,
        num: that.goodsnum[index].num ||1 ,
        oldpric: that.gooodlist[7].originalPrice,
        price: that.gooodlist[7].price,
        name: that.gooodlist[7].name,
        coverPic: that.gooodlist[7].coverPic
      })
    }else if (index == 8) {
      that.payData.push({
        id: that.gooodlist[8].id,
        num: that.goodsnum[index].num ||1 ,
        oldpric: that.gooodlist[8].originalPrice,
        price: that.gooodlist[8].price,
        name: that.gooodlist[8].name,
        coverPic: that.gooodlist[8].coverPic
      })
    }else {
      this.payData.push({
        id: that.gooodlist[index].id,
        num: that.goodsnum[index].num || 1,
        oldpric: that.gooodlist[index].originalPrice,
        price: that.gooodlist[index].price,
        name: that.gooodlist[index].name,
        coverPic: that.gooodlist[index].coverPic
      })
    }
    ///倒叙排列
    let arrss = this.data.payData.sort(function (a, b) {
      return (b.num - a.num)
    });
    //去重
    const hasObj = {}
    const arr1 = arrss.reduce((total, next) => {
      const filterKey = next.id
      hasObj[filterKey] ? '' : hasObj[filterKey] = true && total.push(next)
      return total
    }, [])
    this.setData({
      aarr:arr1
    })
    this.setData({
      aarrlength:this.data.aarr.length
    })
    console.log(arr1);
  },
  // 去结算
handletobuy(){
  if(this.data.aarr.length){
console.log(this.data.aarr);
    var detailsData = JSON.stringify(this.data.aarr);
    detailsData = encodeURIComponent(detailsData);
    wx.navigateTo({
      url: `/pages/zdseepg/index?arr=${detailsData}&sum=${JSON.stringify(this.data.zonjia)}&type=4&upgradeLevel=0`
    })
  }else{
    wx.showToast({
      title: '你还未选购商品',
      icon: 'error',
      duration: 1000,
      mask: true
    })
  }
},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})